import pygame

from PyPixelCore.PyPixelController import PyPixelController
from PyPixelCore.Setting import *
from PyPixelCore.block.Bar import Bar
from PyPixelCore.block.Block import Block
from PyPixelCore.block.DynamicImageBlock import DynamicImageBlock
from PyPixelCore.block.MultiLineTextBlock import MultiLineTextBlock
from PyPixelCore.block.SelectButton import SelectButton
from PyPixelCore.block.SimplifyButton import SimplifyButton
from PyPixelCore.block.TextBlock import TextBlock
from PyPixelCore.block.TextButton import TextButton
from PyPixelCore.panel.EditTextOld import EditTextOld
from PyPixelCore.panel.PanelList import PanelList
from PyPixelCore.panel.Panel import Panel
from PyPixelCore.timer.CycleTimer import CycleTimer
from PyPixelCore.timer.OnlyTimer import OnlyTimer


def change_x(self):  # 测试基础的位置改变
    self.rect.x += 1


def change_x_special_group(self):  # 测试组的特殊位置改变方式
    self.change_point_x_y(point_x=self.rect.x + 1)


def event_v_show_gm(self: PyPixelController):  # v键的控制器事件
    self.groups_manager.print_all_groups()


def disappear(self: Block):  # 测试alpha值
    alpha = self.image.get_alpha() - 2
    if alpha > 0:
        self.image.set_alpha(alpha)
    else:
        self.kill()


def test_func(self: Block):  # 测试计时器动画
    # self.is_show = False  # 只是隐藏起来不参与绘制
    # self.kill()  # 删除
    print("理论3秒之后逐渐消失")
    self.image.set_alpha(255)
    self.set_timer_animation(CycleTimer(1), disappear)
    self.load_image("./recourse/among.png")


def public_timer_test(self):
    pass


def add_s_test(self):  # 测试为文字块添加字符
    text = self.text_content
    text += "s"
    self.set_text(text)


def add_s_test_super(self):  # 测试带动画的添加字符
    text = self.text_content
    text += "s"
    self.set_text(text)
    self.set_limit_size(limit_w=self.limit_w-5)


def test_frame_func(self: Block):  # 测试基础的位置改变
    self.rect.x += 1
    # self.image = pygame.transform.scale(self.image, (self.image.get_width()+1, self.image.get_height()+1))
    # self.image = pygame.Surface((self.image.get_width()+1, self.image.get_height()+1))
    # self.image = pygame.transform.rotate(self.image, 10)
    # self.image.fill(Color_type.RED)


def test_frequency_timer(self):  # 测试次数计时器
    print("我出现了")
    # self.is_show = not self.is_show


def main():
    # 初始化pygame and 创建控制器
    pc = PyPixelController()
    # 创建精灵块
    # 普通块
    test_block = Block(is_alpha=True)
    test_block.set_frame_animation(test_frame_func)
    test_block.set_timer_animation(OnlyTimer(180), test_func)
    # 动态图像块
    dynamic = DynamicImageBlock(collision=True)
    # dynamic.set_image_dict_from_file(F"./recourse/character")
    # dynamic.add_image_list_from_file("./recourse/character/down", "test")
    # print(dynamic.image_dict)
    # dynamic.add_image_list_from_image("./recourse/among.png", "among", 4, 4, 1, 1)
    # dynamic.set_video_state("among")
    dynamic.add_image_list_from_images("./recourse/among.png", ["l", "r", "w", "d"], 4, 4)
    dynamic.set_video_state("r")
    dynamic.set_collision_rect(50, 50)  # 设置碰撞域
    # dynamic.set_animation_interval(8)
    # 文字块
    text_block = TextBlock("测试文字块:换abcdefghijklmn", 0, 450)
    # text_block.set_color(Color_type.BLACK, Color_type.GREY)
    text_block.set_timer_animation(CycleTimer(40), add_s_test)
    # 自动文字块
    mul_text_block = MultiLineTextBlock(
        "自动换行与手动换行文本测试: \n自动\n换行:", layer=1, limit_w=300, point_y=300, border_width=3, limit_h=100)

    # 超级自动文字块
    # super_mul_text_block = SuperMultiLineTextBlock(
    #     "点击组件获得焦点,滑动滚轮翻滚列表", layer=1,limit_h=140, limit_w=200, point_y=150, point_x=10, border_width=3)
    # "自动换行与手动换行文本测试: \n自动\n换行:\n", layer=1, point_y=300, border_width=3, limit_h=22)
    mul_text_block.set_timer_animation(CycleTimer(20), add_s_test_super)
    # mul_text_block.set_timer_animation(FrequencyTimer(60, 5), test_frequency_timer)
    # 按钮
    text_button = TextButton(is_alpha=True, point_x=70, point_y=60, width=100, height=50)
    text_2_button = TextButton(is_alpha=False, point_x=300, point_y=400, width=100, height=50, layer=1)
    # 简化按钮
    simplify_button = SimplifyButton()
    # 选择按钮
    select_button = SelectButton(point_x=300, point_y=100)
    # select_button.set_frame_animation(change_x)

    # bar
    bar = Bar(point_x=220, point_y=50)

    # 组
    panel = Panel(10, 0, 200, 150)
    panel.add_sprites(text_button, simplify_button, dynamic)
    panel.set_timer_animation(CycleTimer(20), change_x_special_group)
    # panel.add_sprites(py_list)

    # 编辑文本框
    edit_text = EditTextOld(400, 25, 200, 100, font_size=basic_font_size)
    edit_text.set_text("测试主动换行\n,测试自动换行...............................")

    # 列表
    py_list = PanelList(width=150, height=300, point_x=480, point_y=180)
    # py_list = PanelList(width=150, height=300, point_x=0, point_y=0)
    mapper = ["退位", "a", "r", "i", "s", "u", ",", "\n"]
    for key in mapper:
        def temp_change(self: SimplifyButton):
            if self.temp_mapper_key == "退位":
                edit_text.delete_char_at_end()
                # super_mul_text_block.blit_a_char("back")
            else:
                edit_text.append_text_content(self.temp_mapper_key)
                # super_mul_text_block.blit_a_char(self.temp_mapper_key)

        py_list_button = SimplifyButton(text_content=f"    +{key}    ")
        py_list_button.temp_mapper_key = key
        py_list_button.set_click_down_func(temp_change)
        py_list.add_sprites(py_list_button)
    for y in range(10):
        # 简化按钮
        py_list_button = SimplifyButton(text_content=f"无用测试按钮{y}", )
        py_list.add_sprites(py_list_button)
    # py_list.set_timer_animation(CycleTimer(10), change_x_special_group)


    # 添加块到管理组
    # pc.add_block(test_block, text_block, mul_text_block, text_button, simplify_button, select_button)
    pc.add_block(test_block, text_block, mul_text_block, select_button, text_2_button)
    pc.add_block(bar)
    # pc.add_block(super_mul_text_block)
    pc.add_block(panel)
    pc.add_block(py_list)
    pc.add_block(edit_text)

    # 添加事件函数
    pc.add_key_event(pygame.K_v, event_v_show_gm)

    pc.run()


if __name__ == '__main__':
    main()
